<?php
namespace app\jobmanagement\service;

use app\constdir\ApiConst;
use think\Exception;
use app\model\TransferConfig as mTransferConfig;


class TransferConfig
{
    private $mTransferConfig;
    private $dd_user_id;
    public function __construct(){
        $this->mTransferConfig = new mTransferConfig();
        $this->dd_user_id = session('dd_user_id');
    }

    /**
     * @introduction:转账配置信息输出
     * @return array|bool|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author:cxh
     * @date: 2020/7/2 17:34
     */
    public function viewTransferConfig(){
            $data = $this->mTransferConfig
                ->field('id,days_between_withdrawals,guaranteed_cash_withdrawal,maximum_single_cash_withdrawal')
                ->find();
            if(empty($data)){
                return ['code'=> ApiConst::TRANSFER_CONFIG_NO_ERROR_CODE, 'msg'=>ApiConst::TRANSFER_CONFIG_NO_ERROR_MSG];
            }

        return $data;
    }


    /**
     * @introduction:修改转账相关配置项数据
     * @param $param
     * @return array|bool
     * @author:cxh
     * @date: 2020/7/2 17:56
     */
    public function saveTransferConfig($param){

        $data = [
            'days_between_withdrawals' => $param['days_between_withdrawals'],
            'guaranteed_cash_withdrawal' => $param['guaranteed_cash_withdrawal'],
            'maximum_single_cash_withdrawal' => $param['maximum_single_cash_withdrawal'],
            'dd_user_id' => $this->dd_user_id,
            'updatetime' => date('Y-m-d H:i:s'),
        ];
        $info = $this->mTransferConfig
            ->where('id',$param['id'])
            ->update($data);
        if(!$info){
            return ['code'=> ApiConst::TRANSFER_CONFIG_UPDATE_ERROR_CODE, 'msg'=>ApiConst::TRANSFER_CONFIG_UPDATE_ERROR_MSG];
        }

        return true;

    }



     /* @auther zlq
     * @create_time 2020/7/2 17:20
     * @description 获取提现配置
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWithdrawConfig()
    {
        $res = $this->mTransferConfig->find();
        if ($res) {
            return $res->toArray();
        } else {
            return [
                'days_between_withdrawals' => 0,
                'guaranteed_cash_withdrawal' => 0.00,
                'maximum_single_cash_withdrawal' => 0.00,
            ];
        }

    }

}




























